package com.tjetc.service.impl;

import com.tjetc.domain.RankRecord;
import com.tjetc.mapper.RankRecordMapper;
import com.tjetc.vo.AlternateInfoVo;
import com.tjetc.vo.RankDTO;
import com.tjetc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.mapper.AlternateMapper;
import com.tjetc.domain.Alternate;
import com.tjetc.service.AlternateService;

import java.util.Date;
import java.util.List;

@Service
public class AlternateServiceImpl implements AlternateService{

    @Resource
    private AlternateMapper alternateMapper;

    @Autowired
    private RankRecordMapper rankRecordMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return alternateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Alternate record) {
        return alternateMapper.insert(record);
    }

    @Override
    public int insertSelective(Alternate record) {
        return alternateMapper.insertSelective(record);
    }

    @Override
    public Alternate selectByPrimaryKey(Integer id) {
        return alternateMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Alternate record) {
        return alternateMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Alternate record) {
        return alternateMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result addAlternate(RankDTO rankDTO) {
        Alternate oldAlternate = alternateMapper.selectByPidAndSid(rankDTO.getPid(),rankDTO.getScheduleId());
        if (oldAlternate != null){
            return Result.fail("你已经候补过了");
        }
        //查看是否有挂号成功的记录
        RankRecord record = rankRecordMapper.selectBySidAndPid(rankDTO.getScheduleId(),rankDTO.getPid());
        if (record != null){
            return Result.fail("你已经预约成功,无需候补");
        }
        Alternate alternate = Alternate.builder().pid(rankDTO.getPid()).scheduleId(rankDTO.getScheduleId()).time(new Date()).isSuccess(0).build();
        int rows = alternateMapper.insert(alternate);
        return rows == 1 ? Result.ok() : Result.fail("候补失败");
    }

    @Override
    public Result getAlternateInfo(Integer id) {
        List<AlternateInfoVo> alternateInfoVos = alternateMapper.getAlternateInfo(id);
        for (AlternateInfoVo alternateInfoVo : alternateInfoVos) {
            if (alternateInfoVo.getIsSuccess() == 0 && alternateInfoVo.getTime().before(new Date())){
                alternateInfoVo.setIsSuccess(3);
            }
        }
        return Result.ok(alternateInfoVos);
    }

    @Override
    public Result cancelAlternate(Integer id) {
        Alternate alternate = alternateMapper.selectByPrimaryKey(id);
        if (alternate == null){
            return Result.fail("候补记录不存在");
        }
        alternate.setIsSuccess(2);
        int rows = alternateMapper.updateByPrimaryKey(alternate);
        return rows == 1 ? Result.ok() : Result.fail("取消候补失败");
    }

}
